package com.cntaiping.ttnews.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.VideoView;

import com.cntaiping.ttnews.R;

import java.io.IOException;

/**
 * Created by Administrator on 2016/7/23.
 */
public class VideoDetailActivity  extends Activity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {
    private String mp4;
    private VideoView mVideoView;
    private MediaPlayer mMediaPlayer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_detail);
        Intent intent = getIntent();
        mp4 = intent.getStringExtra("mp4");

        SurfaceView mSurfaceView = (SurfaceView) findViewById(R.id.surface_vv_detail);
        mMediaPlayer = new MediaPlayer();
        //surfaceView的管理类
        SurfaceHolder surfaceHolder = mSurfaceView.getHolder();
        //添加声明周期的接口回调
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            //surface创建完成时候。在这里做一些初始化的设置
            //比如，设置视频的显示，设置视频源。。
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mMediaPlayer.setDisplay(holder);

                try {
                    //得到asset目录下的音频文件
                    //AssetFileDescriptor assetFileDescriptor = getAssets().openFd("shabc.mp4");
                    //设置数据源
                    //mMediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
                    mMediaPlayer.setDataSource(mp4);
                    mMediaPlayer.prepareAsync();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //surface内部改变的时候回调用此方法
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            //surface销毁时候
            //释放资源的操作，在这里执行。
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                if (mMediaPlayer != null) {
                    mMediaPlayer.pause();
                    mMediaPlayer.release();
                    mMediaPlayer = null;
                }
            }
        });
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnErrorListener(this);
        mMediaPlayer.setOnCompletionListener(this);

        mSurfaceView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //MotionEvent交给手势探测类GestureDetector类来处理
                mDetector.onTouchEvent(event);
                return true;
            }
        });
    }


    GestureDetector mDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
        //        //单击的回调
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            Log.i("tag", "点击了。");


            return super.onSingleTapUp(e);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.i("tag", "双击。。。");
            if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
                mMediaPlayer.pause();
            } else if (mMediaPlayer != null) {
                mMediaPlayer.start();
            }
            return super.onDoubleTap(e);
        }
    });


    //准备好的监听的方法
    @Override
    public void onPrepared(MediaPlayer mp) {
        mMediaPlayer.start();
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        return false;
    }

    @Override
    public void onCompletion(MediaPlayer mp) {

    }


}
